package com.css.fxfzypg.modules.resultupload.repository.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.SettingSeismic.vo.SettingSeismicDetailsVO;
import com.css.fxfzypg.modules.resultManagement.reportManagement.entity.PrefectureResultEntityV2;
import com.css.fxfzypg.modules.resultupload.repository.ResultRepository;
import com.css.fxfzypg.modules.resultupload.repository.entity.PrefectureResultEntity;
import com.css.fxfzypg.modules.resultupload.vo.ParamVo;
import com.css.fxfzypg.modules.resultupload.vo.TaskVo;

import com.css.fxfzypg.modules.resultupload.repository.entity.ResultEntity;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ResultRepositoryImpl implements ResultRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean savePrefectureResult(PrefectureResultEntity resultEntity) {
        String sql = "insert into  ypg_prefecture_result   values(:id, :taskCode, :province, :provinceId, :city, :cityId, :area, :areaId," +
                " :magnitude, :deathToll, :injuredNum, :resettledNum, :rescueForce, :tentNum, :delFlag, :createName, :createId, :createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(resultEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public Map<String, Object> getTaskList(TaskVo taskVo, String userId, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select t1.id,t1.task_num,t1.task_name,t1.task_manager,t1.group_name,t1.task_district from ypg_preassess_task t1 left join ypg_group t2 on t2.id = t1.group_id left join ypg_group_user t3 on t3.group_id = t2.id ");
        StringBuilder whereSql = new StringBuilder("where t1.del_flag = '0' and t1.task_status = '1' and t1.parent_id = '0' and (t1.create_user = '"+userId+"' or t2.group_leader_id = '"+userId+"' or t3.user_id ='"+userId+ "')");
        if(!PlatformObjectUtils.isEmpty(taskVo.getTaskNum())){
            whereSql.append(" and t1.task_num like '%"+ taskVo.getTaskNum() +"%'");
        }
        if(!PlatformObjectUtils.isEmpty(taskVo.getTaskName())){
            whereSql.append(" and t1.task_name like '%"+ taskVo.getTaskName() +"%'");
        }
        if(!PlatformObjectUtils.isEmpty(taskVo.getProvince()) || !PlatformObjectUtils.isEmpty(taskVo.getCity()) || !PlatformObjectUtils.isEmpty(taskVo.getCounty())){
            if(!PlatformObjectUtils.isEmpty(taskVo.getProvince())){
                whereSql.append(" and t1.task_district like '%"+ taskVo.getProvince() +"%'");
            }
            if(!PlatformObjectUtils.isEmpty(taskVo.getCity())){
                whereSql.append(" and t1.task_district like '%"+ taskVo.getCity() +"%'");
            }
            if(!PlatformObjectUtils.isEmpty(taskVo.getCounty())){
                whereSql.append(" and t1.task_district like '%"+ taskVo.getCounty() +"%'");
            }
        }
        StringBuilder orderSql = new StringBuilder(" order by t1.create_time desc");
        int firstIndex=(curPage-1) * pageSize;
        int lastIndex=pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        StringBuilder groupSql = new StringBuilder(" group by t1.id,t1.task_num,t1.task_name,t1.task_manager,t1.group_name,t1.task_district,t1.create_time ");
        sql.append(whereSql).append(groupSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(TaskVo.class)).size();
        List<TaskVo> task = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(TaskVo.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",task);
        return map;
    }

    @Override
    public List<PrefectureResultEntity> getPrefectureResult(String taskCode) {
        String sql = "select * from ypg_prefecture_result where del_flag='0' and task_code='"+taskCode+"'";
        List<PrefectureResultEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PrefectureResultEntity.class));
        return list;
    }

    @Override
    public boolean deletePrefectureResult(String taskCode) {
        String sql = "delete from ypg_prefecture_result where task_code =?";
        return jdbcTemplate.update(sql,taskCode) > 0;
    }

    @Override
    public Integer prefectureResultCount(String taskCode) {
        String sql = "select count(*) count from ypg_prefecture_result where task_code =?";
        return jdbcTemplate.queryForObject(sql, Integer.class,taskCode);
    }

    @Override
    public boolean saveResult(ResultEntity entity) {
        String sql = "insert into  ypg_result values(:id, :taskCode, :sort, :longitude, :latitude, :macroPosition, :magnitude, :disasterArea, :disasterNum," +
                " :disasterGDP, :disasterLoss, :deathToll, :injuredNum, :resettledNum, :rescueForce, :tentNum, " +
                ":delFlag, :createName, :createId, :createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public int resultCount(String taskCode) {
        String sql = "select count(*) count from ypg_result where task_code =?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class,taskCode);
        return count;
    }

    @Override
    public boolean deleteResult(String taskCode) {
        String sql = "delete from ypg_result where del_flag='0' and task_code =?";
        return jdbcTemplate.update(sql,taskCode) > 0;
    }

    @Override
    public List<ParamVo> getResult(String taskCode) {
        String sql = "select sort,longitude,latitude,macro_position from ypg_result where del_flag='0' and task_code='"+taskCode+"' group by sort,longitude,latitude,macro_position";
        List<ParamVo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ParamVo.class));
        return list;
    }

    @Override
    public List<ResultEntity> queryResultData(String taskCode, ParamVo paramVo) {
        String sql = "select * from ypg_result where del_flag='0' and task_code='"+taskCode+"' and sort = ? and longitude= ? and latitude = ? and macro_position = ?";
        List<ResultEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ResultEntity.class),paramVo.getSort(),paramVo.getLongitude(),paramVo.getLatitude(),paramVo.getMacroPosition());
        return list;
    }

    @Override
    public List<ResultEntity> getResultByTaskId(String taskId) {
        String sql = "select * from ypg_result where del_flag='"+YNEnum.N.toString()+"' and task_code= (select task_num from ypg_preassess_task where id='"+taskId+"' and del_flag='"+ YNEnum.N.toString() +"') ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ResultEntity.class));
    }

    @Override
    public List<ResultEntity> getResultByTaskId(List<String> taskIdList) {
        String sql = "select * from ypg_result where del_flag='"+YNEnum.N.toString()+"' and task_code in (select task_num from ypg_preassess_task where id in (:taskIdList) and del_flag='"+ YNEnum.N.toString() +"') ";
        Map idMap = new HashMap();
        idMap.put("taskIdList", taskIdList);
        return namedParameterJdbcTemplate.query(sql, idMap, new BeanPropertyRowMapper<>(ResultEntity.class));
    }

    @Override
    public List<PrefectureResultEntity> getUploadResult(String taskCode) {
        String sql = "select * from ypg_prefecture_result where del_flag='"+YNEnum.N.toString()+"' and task_code='"+taskCode+"' order by city,magnitude";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PrefectureResultEntity.class));
    }

    @Override
    public List<PrefectureResultEntity> getUploadResult(List<String> taskCodeList) {
        String sql = "select * from ypg_prefecture_result where del_flag='"+YNEnum.N.toString()+"' and task_code in (:taskCodeList) order by city,magnitude";
        Map idMap = new HashMap();
        idMap.put("taskCodeList", taskCodeList);
        return namedParameterJdbcTemplate.query(sql, idMap, new BeanPropertyRowMapper<>(PrefectureResultEntity.class));
    }
}
